package com.cc.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cc.entity.UserSchool;
import com.cc.entity.UserStudent;
import com.cc.mongo.TUser;
import com.cc.service.FnService;
import com.cc.service.UserStudentService;
import com.cc.utils.AutherUtils;

@Controller  
@RequestMapping("/userStudent") 
public class UserStudentController {
	@Resource  
	protected UserStudentService userStudentService;
	
	@Resource  
	protected FnService fnService;
	
	@ResponseBody
	@RequestMapping("/sign")  
    public String toIndex(HttpServletRequest request,Model model,UserStudent userStudent){  
        int state=userStudentService.sign(userStudent);
    	return state+"";  
    }
	@ResponseBody
	@RequestMapping("/login")  
    public String login(HttpServletRequest request,HttpSession httpSession,Model model,UserStudent userStudent){  
        int state=userStudentService.login(userStudent);
        if(state==0){
          httpSession.setAttribute("token", userStudent);
          System.out.println("++++++++++++"+httpSession.getId()+"+++++++++++++");
        }
         return state+"";  
    }
	
	@ResponseBody
	@RequestMapping("/private/personinfo")  
    public String personinfo(HttpServletRequest request,HttpSession httpSession,Model model,UserStudent userStudent){  
		UserStudent token=(UserStudent)httpSession.getAttribute("token");
		UserStudent currentUser=fnService.getCurrentUserStudent(token);
		ObjectMapper jsonmapper=new ObjectMapper(); 
		String result="";
		try {
			result=jsonmapper.writeValueAsString(currentUser);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
    }
	
	/**
	 * 修改用户属性值
	 * @param request
	 * @param httpSession
	 * @param model
	 * @param userStudent
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/private/reset")  
    public String reset(HttpServletRequest request,HttpSession httpSession,Model model,String key,String val,String lab){  
		UserStudent token=(UserStudent)httpSession.getAttribute("token");
		UserStudent currentUser=fnService.getCurrentUserStudent(token);
		int state=userStudentService.resetUserStudent(key, val, currentUser,lab);
		return state+"";
    }

}
